package com.xdwanj.servlet;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.util.ConstantsRes;
import com.xdwanj.util.UrlUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/page")
public class PageServlet extends HttpServlet {
    /**
     * 根请求地址
     */
    public static final String REQUEST_MAPPING = "page";

    /**
     * 页面定位参数名
     */
    public static final String POSITION = "position";
    /**
     * 网页动作
     */
    public static final String METHOD = "method";

    /**
     * 主页
     */
    public static final String INDEX = "index";
    /**
     * 登陆页面
     */
    public static final String LOGIN = "login";
    /**
     * 关于页面
     */
    public static final String ABOUT = "about";
    /**
     * 科普页面
     */
    public static final String CLASSROOM = "classroom";
    /**
     * 新闻页面
     */
    public static final String NEWS = "news";
    /**
     * 用户主页
     */
    public static final String USER = "user";
    /**
     * 商城页面
     */
    public static final String SHOP = "shop";
    /**
     * 商品页面
     */
    public static final String COMMODITY = "commodity";
    /**
     * 密码更改页面
     */
    public static final String PWD_PAGE = "pwdPage";
    /**
     * 宠物添加页面
     */
    public static final String ADD_PET = "addPet";
    /**
     * 具体科普页面
     */
    public static final String STUDY = "study";


    /**
     * 用户UID
     */
    public static final String UID = "uid";
    /**
     * 商品ID
     */
    public static final String COID = "coid";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入PageServlet...");
        String position = req.getParameter(POSITION);

        switch (position) {
            case LOGIN:
                forwardLogin(req, resp);
                break;
            case PWD_PAGE:
                forwardPwd(req, resp);
                break;
            case INDEX:
                forwardIndex(req, resp);
                break;
            case CLASSROOM:
                forwardClassroom(req, resp);
                break;
            case STUDY:
                forwardStudy(req, resp);
                break;
            case ABOUT:
                forwardAbout(req, resp);
                break;
            case SHOP:
                forwardShop(req, resp);
                break;
            case COMMODITY:
                forwardCommodity(req, resp);
                break;
            case USER:
                forwardUser(req, resp);
                break;
            case ADD_PET:
                forwardAddPet(req, resp);
                break;
            default:
                // resp.sendRedirect(ConstantsRes.ERROR_PAGE);
        }
    }

    private void forwardStudy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.STUDY_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 转发至宠物添加页面
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void forwardAddPet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String url = null;

        url = ConstantsRes.PET_ADD_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 转发至密码修改页面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void forwardPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.PWDMODIFY_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 转发到商品页面
     *
     * @param req
     * @param resp
     */
    private void forwardCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        String coidStr = req.getParameter(COID);
        int coid = 0;

        if (StringUtils.isStrictlyNumeric(coidStr)) {
            coid = Integer.parseInt(coidStr);
        }

        // 查询单个商品内容
        url = UrlUtil.getUrl(CommodityServlet.REQUEST_MAPPING, new String[]{CommodityServlet.METHOD, CommodityServlet.QUERY, PageServlet.COID, coid + ""});
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);

    }

    /**
     * 用户页面转发
     *
     * @param req
     * @param resp
     */
    private void forwardUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String url = null;
        String method = req.getParameter(METHOD);

        // method == null || method == "petList"
        if (StringUtils.isNullOrEmpty(method) || UserServlet.PET_LIST.equals(method)) {
            url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.PET_LIST});
            System.out.println("url =" + url);
            req.getRequestDispatcher(url).forward(req, resp);
            return;
        }


        switch (method) {
            case UserServlet.SHOPPING_CART:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.SHOPPING_CART});
                System.out.println("url = " + url);
                req.getRequestDispatcher(url).forward(req, resp);
                break;
            case UserServlet.ADD_PET:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.ADD_PET});
                System.out.println("url = " + url);
                break;
            case UserServlet.DEL_PET:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.DEL_PET});
                System.out.println("url = " + url);
                break;
            case UserServlet.UPDATE_PET:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.UPDATE_PET});
                System.out.println("url = " + url);
                break;
            case UserServlet.DEL_SHOPPING_CART:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.DEL_SHOPPING_CART});
                System.out.println("url = " + url);
                break;
            case UserServlet.CONVERSION:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.CONVERSION});
                System.out.println("url = " + url);
                break;
            case UserServlet.ORDER_LIST:
                url = UrlUtil.getUrl(UserServlet.REQUEST_MAPPING, new String[]{UserServlet.METHOD, UserServlet.ORDER_LIST});
                System.out.println("url = " + url);
                break;
            default:
                break;
        }
    }

    /**
     * 商城页面转发,通过转发到ShopServlet中获取数据
     *
     * @param req
     * @param resp
     */
    private void forwardShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;
        String method = req.getParameter(METHOD);

        if (StringUtils.isNullOrEmpty(method) || ShopServlet.RECOMMEND.equals(method)) {
            url = UrlUtil.getUrl(ShopServlet.REQUEST_MAPPING, new String[]{ShopServlet.METHOD, ShopServlet.RECOMMEND});
            System.out.println("url = " + url);
            req.getRequestDispatcher(url).forward(req, resp);
        }


    }

    /**
     * 关于我们页面转发
     *
     * @param req
     * @param resp
     */
    private void forwardAbout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        // TODO:关于我们未完善
        url = ConstantsRes.ABOUT_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 科普页面转发
     *
     * @param req
     * @param resp
     */
    private void forwardClassroom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.CLASSROOM_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 主页转发
     *
     * @param req
     * @param resp
     */
    private void forwardIndex(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.INDEX_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 登陆页面转发
     *
     * @param req
     * @param resp
     */
    private void forwardLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.LOGIN_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}